package tr.edu.metu.is.mega.client;

import tr.edu.metu.is.mega.client.core.AuthUtils;
import tr.edu.metu.is.mega.client.core.LoginCallbackHandler;
import tr.edu.metu.is.mega.common.dto.UserDTO;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point of MEGA system.
 */
public class MEGA implements EntryPoint, LoginCallbackHandler {

    private RootPanel rootPanel;

    /**
     * {@inheritDoc}
     */
    @Override
    public void onModuleLoad() {
        rootPanel = RootPanel.get("root");
        // login user
         AuthUtils.getInstance().login(this);
//        GwtAuthenticationService.Util.getInstance().login("r", "r", new AsyncCallbackAdapter<UserDTO>() {
//
//            @Override
//            public void onSuccess(UserDTO result) {
//                rootPanel.add(new EditProfileWindow());
//            }
//        });
        // rootPanel.add(new StudentCourseWindow());
        // GwtAuthenticationService.Util.getInstance().login("r", "r", new
        // AsyncCallback<UserDTO>() {
        //
        // @Override
        // public void onSuccess(UserDTO result) {
        // CourseDTO dto = new CourseDTO();
        // dto.setPkid(1);
        // GwtCourseService.Util.getInstance().getCourses(dto, new
        // AsyncCallback<List<CourseDTO>>() {
        //
        // @Override
        // public void onSuccess(List<CourseDTO> result) {
        // if (result.size() == 1) {
        // SelectionEventBus.getInstance().fireEvent(CourseChangeEvent.UPDATE,
        // result.get(0));
        // }
        // }
        //
        // @Override
        // public void onFailure(Throwable caught) {
        // // TODO Auto-generated method stub
        //
        // }
        // });
        // }
        //
        // @Override
        // public void onFailure(Throwable caught) {
        // // TODO Auto-generated method stub
        //
        // }
        // });

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void loggedIn(UserDTO user) {
        // initiate main frame
        MEGAMainFrame mainFrame = new MEGAMainFrame();
        mainFrame.setSize(1024, 720);

        // add main frame to the root panel
        if (rootPanel.getWidgetCount() > 0) {
            rootPanel.remove(0);
        }
        rootPanel.add(mainFrame, 0, 0);
    }
}
